/**
 * Copyright (C) 2011  Robin Dietrich, Nils Kenneweg, Leonard Stutz
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/ 

#ifndef __SINGLETON_HPP_
#define __SINGLETON_HPP_

#include <cassert>
#include <cstddef>

/** singleton template for easy singleton generation */
template <class C> class Singleton {
    public:
        /** creates a new instance of an object or returns the existing one */
        static C* Instance() {
            if(!obj_instance) {
                obj_instance = new C;
            }
            assert(obj_instance !=NULL);
            return obj_instance;
        }

        /** destroys / free the memory of an object */
        static void Destroy() {
            if (obj_instance) {
                delete obj_instance;
                obj_instance=NULL;
            }
        }
    protected:
        Singleton();
        ~Singleton();
    private:
        Singleton(Singleton const&);
        Singleton& operator=(Singleton const&);
        static C* obj_instance;
};

template <class C> C* Singleton<C>::obj_instance=NULL;

#endif
